package com.jintian.smart.kernel.common.expr.op.define;

import com.alibaba.fastjson.util.TypeUtils;
import com.jintian.smart.kernel.common.expr.datameta.BaseDataMeta;
import com.jintian.smart.kernel.common.expr.datameta.Constant;
import com.jintian.smart.kernel.common.expr.op.Operator;
import com.jintian.smart.kernel.core.expr.IllegalExpressionException;

/**
 * 选择操作
 */
public class Op_SELECT extends Op_Base {
    public Op_SELECT() {
        super(Operator.SELECT);
    }

    public Constant execute(Constant[] args) throws IllegalExpressionException {
        Object[] params = this.getParameterValues(args);
        boolean b = TypeUtils.castToBoolean(params[0]);
        return new Constant(BaseDataMeta.DataType.DATATYPE_OBJECT, b ? params[1] : params[2]);
    }

    public Constant verify(int opPositin, BaseDataMeta[] args) throws IllegalExpressionException {
        this.checkParameterCount(opPositin, args);
        if (!args[2].getDataType().equals(BaseDataMeta.DataType.DATATYPE_BOOLEAN)
                && !args[2].getDataType().equals(BaseDataMeta.DataType.DATATYPE_OBJECT)) {
            throw new IllegalExpressionException(
                    "操作符\"" + this.getOperator().getToken() + "\"第一个参数类型错误，应为Boolean或者Object",
                    this.getOperator().getToken(), opPositin);
        }
        return new Constant(BaseDataMeta.DataType.DATATYPE_OBJECT, null);
    }
}
